//
//  MTProgressHUD.swift
//  Buyer
//
//  Created by DavidYang on 16/7/18.
//  Copyright © 2016年 ymatou. All rights reserved.
//

import UIKit
import Cartography
import MBProgressHUD

open class MTProgressHUD {

    //MARK:Public methods
    /**
     Show loadingView，you need to invoke hideHUDForView manually to remove the HUD
     
     - parameter message:                prompt message
     - parameter toView:                 HUD's superview
     - parameter allowTouchEvent:         allow user interactions while HUD is displayed， defaults to true
     */
    open class func showMessage(_ message: String, toView:UIView?, allowTouchEvent: Bool = true){
        guard toView != nil else {return}
        let hud = MBProgressHUD.showAdded(to: toView!, animated: false)
        hud.label.text = message
        hud.label.numberOfLines = 0
        hud.isUserInteractionEnabled = !allowTouchEvent
        var images: [UIImage] = []
        for index in 0 ... 25 {
            let image = UIImage.init(named:String.init(format: "透明度三点_000%02d", index))
            images.append(image!)
        }
        let imageView = UIImageView()
        imageView.animationImages = images
        imageView.animationDuration = 1
        imageView.startAnimating()
        hud.bezelView.layer.masksToBounds = false
        hud.customView = imageView
        hud.mode = MBProgressHUDMode.customView
        hud.contentColor = .white
        hud.bezelView.style = MBProgressHUDBackgroundStyle.solidColor
        hud.bezelView.color = UIColor.clear
        hud.bezelView.layer.cornerRadius = 2
        if !message.isEmpty {
            hud.bezelView.color = COLOR_HEX(hexString: "0x000000", alpha: 0.8)
        }
    }
    /**
     Remove view's first HUD
    */
    open class func hideHUDForView(_ view: UIView?) {
        guard view != nil else {return}
        MBProgressHUD.hide(for: view!, animated: true)
    }
    
    /**
     Success tip,removed automatically
     */
    open class func showSuccess(_ message: String,toView:UIView?) {
        MTProgressHUD.show(message,icon: "success.png",toView: toView)
    }
    
    /**
     Error tip,removed automatically
     */
    open class func showError(_ message: String,toView:UIView?) {
        MTProgressHUD.show(message, icon: "error.png", toView: toView)
    }
    
    /**
     Warning tip,removed automatically
     */
    open class func showInfo(_ message: String,toView:UIView?) {
        MTProgressHUD.show(message, icon: "info.png", toView: toView)
    }
    
    
    
    //MARK:Private methods
    fileprivate class func show(_ message: String, icon: String, toView: UIView?) {
        var view = toView
        if view == nil {
            view = UIApplication.shared.delegate?.window!!
        }
        
        let hud = MBProgressHUD.showAdded(to: view!, animated: false)
        hud.label.text = message
        hud.label.numberOfLines = 0
        hud.isUserInteractionEnabled = false
        if let image = UIImage.init(named: "MBProgressHUD.bundle/\(icon)") {
           hud.customView = UIImageView.init(image: image)
        }
        hud.mode = MBProgressHUDMode.customView
        hud.contentColor = .white
        hud.bezelView.layer.masksToBounds = false
        hud.bezelView.style = MBProgressHUDBackgroundStyle.solidColor
        hud.bezelView.color = COLOR_HEX(hexString: "0x000000", alpha: 0.8)
        hud.bezelView.layer.cornerRadius = 2
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            UIView.animate(withDuration: 0.2, animations: {
                hud.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
            }) { (finished) in
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                    hud.removeFromSuperview()
                }
            }
        }
    }
}
